package edu.dali.enterprise_test.controller.menu;

import com.github.pagehelper.PageHelper;
import edu.dali.enterprise_test.dao.mapper.classification.MainMapper;
import edu.dali.enterprise_test.model.Pager;
import edu.dali.enterprise_test.model.vo.Dto;
import edu.dali.enterprise_test.service.classification.MainService;
import edu.dali.enterprise_test.service.classification.SubService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/subc")
public class SubcController {

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private SubService subService;

    @Autowired
    private MainMapper mainMapper;

    @RequestMapping("index")
    public String index() {
        return "sub/index";
    }

    @RequestMapping("getData")
    @ResponseBody
    public Pager<Dto> getData(int page , int limit) {
        PageHelper.startPage(page,limit);
        Dto params = new Dto();
        return subService.selectList(params);
    }

    @RequestMapping("addDialog")
    public String addDialog(Model model) {
        List<Dto> mainclass = mainMapper.selectList(null);
        model.addAttribute("mainclass" , mainclass);
        return "sub/add";
    }

    @RequestMapping("updateDialog/{id}")
    public String updateDialog(@PathVariable String id , Model model) {
        Dto dto = subService.selectOne(id);
        List<Dto> mainclass = mainMapper.selectList(null);
        model.addAttribute("mainclass" , mainclass);
        model.addAttribute("info" , dto);
        return "sub/update";
    }

    @PostMapping("insert")
    @ResponseBody
    public Map<String , Object> insert(String cname , String cid , String description) {
        Map<String , Object> res = new HashMap<>();
        Dto dto = new Dto();
        dto.put("cname" , cname);
        dto.put("cid" , cid);
        dto.put("description" , description);
        subService.insert(dto);
        res.put("code" , 0);
        res.put("msg" , "添加成功");
        return res;
    }
    @PostMapping("update")
    @ResponseBody
    public Map<String , Object> update(String id ,String cname , String cid ,  String description) {
        Map<String , Object> res = new HashMap<>();
        Dto dto = new Dto();
        dto.put("id" , id);
        dto.put("item" , cname);
        dto.put("cid" , cid);
        dto.put("description" , description);
        subService.update(dto);

        res.put("code" , 0);
        res.put("msg" , "添加成功");
        return res;
    }

    @PostMapping("delete")
    @ResponseBody
    public Map<String , Object> delete(String id) {
        Map<String , Object> res = new HashMap<>();
        subService.delete(id);
        res.put("code" , 0);
        res.put("msg" , "删除成功");
        return res;
    }
}
